package cn.bxshey.resourceSharing.service.impl;

import cn.bxshey.resourceSharing.entity.Comments;
import cn.bxshey.resourceSharing.entity.Essay;
import cn.bxshey.resourceSharing.entity.User;
import cn.bxshey.resourceSharing.mapper.CommentsMapper;
import cn.bxshey.resourceSharing.mapper.EssayMapper;
import cn.bxshey.resourceSharing.mapper.UserMapper;
import cn.bxshey.resourceSharing.service.CommentsService;
import cn.bxshey.resourceSharing.utils.RedisUtils;
import cn.bxshey.resourceSharing.vo.comments.PageVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (Comments)表服务实现类
 *
 * @author makejava
 * @since 2023-12-16 17:54:44
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {

    @Autowired
    CommentsMapper commentsMapper;

    @Autowired
    RedisUtils redisUtils;


    /**
     * 查询层级评论
     * @param pid 层级 0顶级  1下级
     * @param current 第几页 1是第一页
     * @param size 查询几条
     * @param eid 文章id
     * @return 评论集合
     */
    @Override
    public PageVO selectComments(Integer pid, Integer current, Integer size, Long eid) {
        PageVO page = new PageVO();
//        Long count = commentsMapper.countComments(pid, (current-1)*size, size, eid);
//        page.setPage((int) (count%size==0 ? count/size : (count/size)+1));
//        page.setCount(count);
        System.out.println(pid);

        System.out.println(eid);
        System.out.println(size);
        System.out.println(current);
//        commentsMapper.selectComments(0L,eid,(current-1)*size,size);
        System.out.println(page);
        return page;
    }


    /**
     * 新增评论
     * @param comments
     * @return 成功数量
     */
    @Override
    public int insert(User user, Comments comments) {
        int num = commentsMapper.insertComments(comments);
        if(comments.getReplyId()!=0 && Long.parseLong(String.valueOf(user.getId())) != comments.getReplyId()){
            redisUtils.addListValue(RedisUtils.COMMENTS,comments.getReplyId(),String.valueOf(comments.getId()));
        }
         return num;
    }

    /**
     * 获取未查看评论信息个数
     * @param:  用户id
     * @return 返回个数
     */
    @Override
    public Long size(Long id) {
        return redisUtils.getSize(RedisUtils.COMMENTS, (String.valueOf(id)));
    }

    /**
     *
     * @param current 第几页
     * @param id 用户ID，redis对应的key
     * @return 返回评论信息
     */
    @Override
    public List<Comments> unread(Long current,Long id) {
         long start = (current-1)*5;
         List<String> listValue = redisUtils.getListValue(RedisUtils.COMMENTS, id, start==0 ? start : start+1 , 5*current);
         if(listValue.size()>0){
             return commentsMapper.getByAllId(listValue);
         }
        return null;
    }

    /**
     *
     * @param id 用户的id
     * @param value 删除的value值
     * @return 返回删除个数
     */
    @Override
    public Long read(Long value,Long id) {
        if(Objects.nonNull(value)){
            return redisUtils.delListValue(RedisUtils.COMMENTS,id,String.valueOf(value));
        }
        redisUtils.delListValue(RedisUtils.COMMENTS,id);
        return 0L;
    }


//    @Override
//    public EssayPageVO selectComments(Integer pid, Integer current, Integer size, Integer eid) {
//        EssayPageVO page = new EssayPageVO();
//        Long count = commentsMapper.countComments(pid, (current-1)*size, size, eid);
//        page.setPage(count%size==0 ? count/size : (count/size)+1);
//        page.setCount(count);
//        page.setRecords(commentsMapper.selectLevel(pid,(current-1)*size,size,eid));
//        return page;
//    }
}

